import {Card, Comb, MAX_HAND_CARD} from "./public/GameDefine";
import {GameLogic} from "./public/GameLogic";

export const HERO_LOCAL_SEAT = 0;
export const CARD_BACK = 0x00;

export class HintData{
    static combs:Array<Comb> = [];
    static canOutCombs:Array<Array<Comb>> = [];
    static idx = 0;
    static preComb:Comb = null;
    static logic:GameLogic = null;
    static init(logic:GameLogic,handCards:Array<number>,preComb:Comb){
        HintData.logic = logic;
        HintData.preComb = preComb;
        let combs = logic.hint_out(handCards,preComb);
        HintData.combs = combs;
        HintData.idx = 0;

        HintData.canOutCombs = [];
        for(let i=0;i<combs.length;i++){
            let len = combs[i].cards.length;
            if(HintData.canOutCombs[len] == null){
                HintData.canOutCombs[len] = [];
            }
            HintData.canOutCombs[len].push(combs[i]);
        }
    }
    static getNextHint():Comb{
        if(HintData.idx>=HintData.combs.length){
            HintData.idx = 0;
        }
        return HintData.combs[HintData.idx++];
    }

    static getCardsBySelectCard(upCards:Array<number>){
        if(upCards.length == 0){
            return [];
        }
        //获取相同牌点的合集
        function getCommonSubset(cards1:Array<number>,cards2:Array<number>){
            let matchList = [];
            let commonCards = [];
            for(let i=0;i<cards1.length;i++){
                let num_i = Card.getSize(cards1[i]);
                for(let j=0;j<cards2.length;j++){
                    if(num_i == Card.getSize(cards2[j]) && matchList[j] == null){
                        matchList[j] = 1;
                        commonCards.push(cards2[j]);
                    }
                }
            }
            return commonCards;
        }

        //删除已有牌点的牌
        function delCardAndNum(upCards,retCards)
        {
            let matchUp = [];
            let matchRet = [];
            //先标记相同的手牌
            for(let i=0;i<upCards.length;i++){
                for(let j=0;j<retCards.length;j++){
                    if(upCards[i] == retCards[j] && matchUp[i] == null && matchRet[j] == null)
                    {
                        matchUp[i] = 1;
                        matchRet[j] = 1;
                        break;
                    }
                }
            }

            //再标记牌点相同的牌
            for(let i=0;i<upCards.length;i++){
                let num_i = Card.getSize(upCards[i]);
                for(let j=0;j<retCards.length;j++){
                    if(num_i == Card.getSize(retCards[j]) && matchUp[i] == null && matchRet[j] == null){
                        matchUp[i] = 1;
                        matchRet[j] = 1;
                        break;
                    }
                }
            }

            let cards = [];
            for(let i=0;i<retCards.length;i++){
                if(matchRet[i] != 1){
                    cards.push(retCards[i]);
                }
            }
            return cards;
        }

        let retCards = [];
        for(let i=upCards.length;i<=MAX_HAND_CARD;i++){
            if(HintData.canOutCombs[i] == null){
                continue;
            }
            let combs = HintData.canOutCombs[i];
            for(let j=0;j<combs.length;j++){
                let cards = combs[j].cards.slice(0);
                if(HintData.logic.is_sub_cards(cards,upCards)){
                    if(retCards.length!=0){
                        retCards = getCommonSubset(retCards,cards);
                    }else{
                        retCards = cards;
                    }
                }
            }
            //刚好找到，则不需要弹起
            if(retCards.length == upCards.length){
                return [];
            }
        }
        //若未找到也不需要弹起
        if(retCards.length==0){
            return [];
        }

        return delCardAndNum(upCards,retCards);
    }

    static getUpByFrameSelectCard(upCards){
        let combs = HintData.logic.hint_out(upCards,HintData.preComb);
        if(combs.length == 0){
            return [];
        }
        let kinds = [];
        for(let i=0;i<combs.length;i++){
            let no_same = HintData.logic.get_no_same_size(combs[i].cards);
            kinds[i] = no_same.length;
        }
        let max=0;
        for(let i=1;i<combs.length;i++){
            if(kinds[max]<kinds[i] ||
                (kinds[max]==kinds[i] && combs[max].cards.length<combs[i].cards.length))
            {
                max = i;
            }
        }

        return combs[max].cards;
    }
}